<?php

/**
 * 后台首页控制器
 * @author:Cloud
 * @copyright:TcitCMS
 * @since:2014-05-12
 */

namespace Home\Controller;

class MemberController extends CommonController
{

    function _initialize()
    {
        parent::_initialize();
    }

    function reg()
    {
        $this->display();
    }

    function login()
    {
        $this->display();
    }

    function loginOut()
    {
        $memberModel = new \Common\Model\MemberModel();
        $memberModel->loginOut();
        $this->success('退出成功', U('Index/index'));
    }

    function forget()
    {
        $this->display();
    }

    function forgetAction()
    {
        $username = I('param.username');
        $phone = I('param.phone');
        $vCode = I('param.vCode');
        $password1 = I('param.password');
        $password2 = I('param.password2');
        if ($password1 != $password2) {
            $this->ajaxReturn('两次输入密码不一致！');
        } elseif (md5($vCode) != session($phone)) {
            $this->ajaxReturn('手机验证码错误！');
        } else {
            $model = new \Common\Model\MemberModel();
            $data = $model->where('`lm_id`=7 and `deleted`=0 and username="' . $username . '" and phone="' . $phone . '" ')->find();
            if (!$data) {
                $this->ajaxReturn('没有找到该用户！');
            }
            $model->where('id=' . $data['id'])->setField('password', md5($password1));
            $model->loginOut();
            $this->ajaxReturn('');
        }
    }

    function loginAction()
    {
        $username = I('post.username');
        $password = I('post.password');
        $model = new \Common\Model\MemberModel();
        $res = $model->LoginAction($username, $password);
        if ($res === true) {
            $this->ajaxReturn('');
        } else {
            $this->ajaxReturn($res);
        }
    }

    function regAction()
    {
        $username = I('param.username');
        $phone = I('param.phone');
        $vCode = I('param.vCode');
        $password1 = I('param.password');
        $password2 = I('param.password2');
        $return = $this->checkUsername($username, FALSE);
        if ($return) {
            $this->ajaxReturn($return);
        } elseif ($password1 != $password2) {
            $this->ajaxReturn('两次输入密码不一致！');
        } elseif (md5($vCode) != session($phone)) {
            $this->ajaxReturn('手机验证码错误！');
        } else {
            $model = new \Common\Model\MemberModel();
            $_POST['status'] = 1;
            $_POST['lm_id'] = 7;
            $_POST['lastdate'] = mktime();
            if ($data = $model->create())
                $model->add();
            else {
                $this->ajaxReturn($model->getError());
            }
            $this->ajaxReturn('');
        }
    }

    function checkUsername($username, $ajax = true)
    {
        $returnData = array('msg' => '');
        $username = $username ? $username : I('param.username');
        if (!$username) {
            $returnData['msg'] = '用户名不能为空！';
        } elseif (mb_strlen($username, "utf-8") > 15 || mb_strlen($username, "utf-8") < 5) {
            $returnData['msg'] = '用户名长度为5到15个字符！';
        } elseif (!preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)) {
            $returnData['msg'] = '用户名格式不对！';
        } else {
            $model = new \Common\Model\MemberModel();
            $data = $model->where('`lm_id`=7 and `deleted`=0 and username="' . $username . '"')->find();
            $data ? $returnData['msg'] = '用户名已经被注册！' : '';
        }
        if ($ajax) {
            $this->ajaxReturn($returnData);
        } else {
            return $returnData['msg'];
        }
    }

    function sendSms()
    {
        $returnData = array(
            'status' => '0',
            'errmsg' => ''
        );
        $phone = I('param.phone');
        $forget = I('param.forget');
        $username = I('param.username');
        if (!$phone) {
            $returnData['errmsg'] = '电话号码不能为空！';
        } elseif ($phone && !\Common\Org\Util::IsPhoneNumber($phone)) {
            $returnData['errmsg'] = '电话号码格式错误！';
        } elseif ($forget && !$username) {
            $returnData['errmsg'] = '请填写用户名！';
        } else {
            if ($forget == 1) {
                $model = new \Common\Model\MemberModel();
                $data = $model->where('`lm_id`=7 and `deleted`=0 and username="' . $username . '" and phone="' . $phone . '" ')->find();
                if (!$data) {
                    $returnData['errmsg'] = '没有找到该用户！';
                    $this->ajaxReturn($returnData);
                }
            } else {
                $model = new \Common\Model\MemberModel();
                $data = $model->where('`lm_id`=7 and `deleted`=0 and phone="' . $phone . '"')->find();
                if ($data) {
                    $returnData['errmsg'] = '手机号已经被注册！';
                    $this->ajaxReturn($returnData);
                }
            }
            $vCode = rand(000000, 999999);
            session($phone, md5($vCode));
            //vendor("AliSMS.SignatureHelper");
            $params = array();
            // fixme 必填：是否启用https
            $security = false;
            $accessKeyId = "LTAIJbYtHEwCeFOw";
            $accessKeySecret = "yXAkOmTx7ygtSapeNCF8ktX0YHiFob";
            $params["PhoneNumbers"] = $phone;
            $params["SignName"] = "新微社";
            $params["TemplateCode"] = "SMS_165040139";
            $params['TemplateParam'] = Array(
                "code" => $vCode,
            );
            // fixme 可选: 设置发送短信流水号
            //$params['OutId'] = "12345";
            // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
            //$params['SmsUpExtendCode'] = "1234567";
            // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
            if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
                $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
            }

            // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
            //$helper = new \AliSMS\SignatureHelper();
            $helper = new \Common\Org\AliSMS\SignatureHelper();

            // 此处可能会抛出异常，注意catch
            $content = $helper->request(
                    $accessKeyId, $accessKeySecret, "dysmsapi.aliyuncs.com", array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
                    )), $security
            );
            if ($content->Code == 'OK') {
                $returnData['status'] = 1;
                $returnData['errmsg'] = '短信发送成功，请注意接收！';
                //$returnData['errmsg'] = $content->Message;
            } else {
                $returnData['status'] = 2;
                $returnData['errmsg'] = '调用短信接口出错，请稍后重试！';
                //$returnData['errmsg'] = $content->Message;
            }
        }
        //print_r($returnData);
        $this->ajaxReturn($returnData);
    }

    function index()
    {
        $model = new \Common\Model\NewsModel();
        $this->display();
    }

}

?>